package javango.contrib.hibernate;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.google.inject.Binder;
import com.google.inject.Module;

/**
 * @author johns
 *
 */
public class HibernateModule implements Module {
	
	Log log = LogFactory.getLog(HibernateModule.class);
	
	public void configure(Binder binder) {

		AnnotationConfiguration configuration = new AnnotationConfiguration();
		
		if (!configuration.getProperties().contains("hibernate.current_session_context_class")) {
			log.info("Defaulting hibernate.current_session_context_class to managed");
			configuration.setProperty("hibernate.current_session_context_class", "managed");
		}
		
		binder.bind(AnnotationConfiguration.class).toInstance(configuration);

		
		SessionFactory sessionFactory = configuration.configure().buildSessionFactory();		
		binder.bind(SessionFactory.class).toInstance(sessionFactory);		
	}

}
